/*  
 * i-OSGi - Tunable Bundle Isolation for OSGi
 * Copyright (C) 2011  Sven Schulz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.iosgi.impl;

import java.net.URI;

import org.iosgi.IsolationAdmin;
import org.iosgi.engine.Operation;

/**
 * @author Sven Schulz
 */
public class IsolationEnvironmentOperation extends AbstractOperation {

	public enum Type {
		CREATE, DESTROY
	}

	private final Type type;

	public IsolationEnvironmentOperation(final Type type, final String target) {
		super(target);
		this.type = type;
	}

	@Override
	public boolean subsumes(final Operation op) {
		if (type == Type.DESTROY && op instanceof AbstractOperation) {
			AbstractOperation aop = (AbstractOperation) op;
			return aop.getTarget().startsWith(this.getTarget());
		}
		return super.subsumes(op);
	}

	@Override
	public void perform(IsolationAdmin admin) throws Exception {
		URI parent = EnvironmentIDs.getParent(URI.create(this.getTarget()));
		switch (type) {
		case CREATE: {
			/* TODO Lookup parent and invoke spawn. */
			break;
		}
		case DESTROY: {
			/* TODO Lookup environment and invoke destroy. */
			break;
		}
		}
	}

	@Override
	public String toString() {
		StringBuilder b = new StringBuilder();
		b.append(super.toString()).append('[').append(type).append(']');
		return b.toString();
	}

}